'use strict'

// 
$(function () {
    getData();
    console.log("%c如果你恰好没有运气，试试勇气！", "color:red")
})

function getData(keyword) {
    let url = 'http://be.biqibaodeatao.top/roles'
    if (keyword) {
        url = url + '?keyword=' + keyword
    }
    let tr = $('[id^=role]');
    tr.remove();
    $.ajax({
        type: 'get',
        url: url,
        success: function (res) {
            res.forEach(item => {
                let html = `
                        <tr id="role${item.id}">
                            <td>${item.id}</td>
                            <td>${item.rolename}</td>
                            <td>${item.email}</td>
                            <td>
                                <input type="button" value="编辑" onclick="btnEdit(${item.id})">
                                <input type="button" value="删除" onclick="btnDel(${item.id})">
                            </td>
                        </tr>`
                let tbRole = $('#tbRole');
                tbRole.append(html);
            })

        }
    })


}

//过滤
function btnFilter() {
    let keyword = $('[name=keyword]').val();
    getData(keyword);
}

//新增
function btnAdd() {
    location.href = 'addOrEdit.html';
}

//编辑
function btnEdit(id) {
    location.href = 'addOrEdit.html?id=' + id;
}

//删除
function btnDel(id) {
    if (confirm('你确认要删除吗？')) {
        $.ajax({
            type: 'delete',
            url: 'http://be.biqibaodeatao.top/roles/' + id,
            success: function (res) {
                $('#role' + id).remove();
            }
        })
    }
}


